package logic

import (
	"context"
	"shopconnect/pkg"
	"shopconnect/shoprpc/shoprpc"
	"strconv"

	"shopconnect/api/internal/svc"
	"shopconnect/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserLoginLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLoginLogic {
	return &UserLoginLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserLoginLogic) UserLogin(req *types.UserLoginRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	login, err := l.svcCtx.ShopRpc.UserLogin(l.ctx, &shoprpc.UserLoginRequest{
		Mobile:   req.Mobile,
		Password: req.Password,
		SendCode: req.SendCode,
	})
	if err != nil {
		return &types.Response{
			Msg:  "登录失败" + err.Error(),
			Data: nil,
			Code: 500,
		}, nil
	}
	handler, err := pkg.TokenHandler(strconv.FormatInt(login.Id, 10))
	if err != nil {
		return &types.Response{
			Msg:  err.Error(),
			Data: nil,
			Code: 500,
		}, nil
	}
	return &types.Response{
		Msg:  "登录成功",
		Data: handler,
		Code: 200,
	}, nil
}
